#include "gtest/gtest.h"
#include "../BaseUtil/MyTest.h"
#include "DOMCoreImp.h"
namespace DOMCoreImp {

class DocumentImpTest : public testing::Test {
protected:
	virtual void SetUp() {
		doc = new DocumentImp();
	}

	virtual void TearDown() {
		delete doc;
	}

	DocumentImp * doc;
};

TEST_F(DocumentImpTest, Inheritance) {
	DocumentPtr(new DocumentImp());
}

TEST_F(DocumentImpTest, NodeType) {
	ASSERT_EQ( Node::DOCUMENT_NODE, doc->getNodeType() );
}

TEST_F(DocumentImpTest, Name) {
	ASSERT_EQ( "#document", doc->getNodeName() );
}

TEST_F(DocumentImpTest, Value) {
	ASSERT_TRUE( doc->getNodeValue().empty() );
	ASSERT_THROW( doc->setNodeValue("aa"), DOMExceptionPtr );
}

TEST_F(DocumentImpTest, Attribute) {
	ASSERT_FALSE( doc->hasAttributes() );
	ASSERT_FALSE( doc->getAttributes() );
}

TEST_F(DocumentImpTest, CreateElement) {
	Element * elem = doc->createElement("ElementName");
	ASSERT_TRUE( elem );
	ASSERT_EQ( doc, elem->getOwnerDocument() );
	ASSERT_STREQ( "ElementName", elem->getNodeName().c_str() );
	ASSERT_FALSE( elem->getParentNode() );
}

TEST_F(DocumentImpTest, CreateAttribute) {
	Attr * attr = doc->createAttribute("AttrName");
	ASSERT_TRUE( attr );
	ASSERT_EQ( doc, attr->getOwnerDocument() );
	ASSERT_STREQ( "AttrName", attr->getNodeName().c_str() );
	ASSERT_FALSE( attr->getParentNode() );
}

TEST_F(DocumentImpTest, CreateText) {
	Text * text = doc->createTextNodeImp("TextData");
	ASSERT_TRUE( text );
	ASSERT_EQ( doc, text->getOwnerDocument() );
	ASSERT_STREQ( "TextData", text->getNodeValue().c_str() );
	ASSERT_FALSE( text->getParentNode() );
}

TEST_F(DocumentImpTest, AcceptElementAsChild) {
	ElementImp * elem = doc->createElementImp("ElementName");
	ASSERT_TRUE( elem );
	ASSERT_EQ( elem, doc->appendChild( elem ) );
	ASSERT_TRUE( doc->getChildNodes() );
	ASSERT_EQ(1ul, doc->getChildNodes()->getLength() );
}

TEST_F(DocumentImpTest, DenyAttrAsChild) {
	AttrImp * attr = doc->createAttributeImp("AttrName");
	ASSERT_TRUE( attr );
	ASSERT_EQ( NULL, doc->appendChild( attr ) );
	ASSERT_TRUE( doc->getChildNodes() );
	ASSERT_EQ( 0ul, doc->getChildNodes()->getLength() );
}

TEST_F(DocumentImpTest, DenyDocumentAsChild) {
	DocumentImp * doc2 = new DocumentImp();
	ASSERT_EQ( NULL, doc->appendChild( doc2 ) );
	ASSERT_EQ( 0ul, doc->getChildNodes()->getLength() );
	delete doc2;
}

TEST_F(DocumentImpTest, DocumentMustHaveOnlyOneChild) {
	ElementImp * elem1 = doc->createElementImp("ElementName1");
	ElementImp * elem2 = doc->createElementImp("ElementName2");
	ASSERT_EQ( elem1, doc->appendChild( elem1 ) );
	ASSERT_EQ( NULL, doc->appendChild( elem2 ) );
	ASSERT_EQ(1ul, doc->getChildNodes()->getLength() );
}

TEST_F(DocumentImpTest, GetDocumentElement) {
	ElementImp * elem = doc->createElementImp("ElementName");
	ASSERT_FALSE( doc->getDocumentElement() );
	doc->appendChild( elem );
	ASSERT_TRUE( doc->getDocumentElement() );
	ASSERT_EQ( elem, doc->getDocumentElement() );
}

TEST_F(DocumentImpTest, GetElementsByTagName) {
//	NodeList * getElementsByTagName(DOMString tagname);
	// TODO
}

TEST_F(DocumentImpTest, GetElementsByTagNameWithAsterisk) {
//	NodeList * getElementsByTagName(DOMString tagname);
	// TODO
}


}
